package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.UserLJMDao;
import com.itheima.pojo.Menu;
import com.itheima.service.UserLJMService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @PackageName: com.itheima.service.impl
 * @ClassName: UserLJMServiceImpl
 * @Author: LiuJunMeng
 * @Date: 2020/2/16 9:28
 * @Description: //TODO
 */
@Service(interfaceClass = UserLJMService.class)
@Transactional
public class UserLJMServiceImpl implements UserLJMService {
    @Autowired
    private UserLJMDao userLJMDao;

    /**
     * 获取用户菜单权限
     *
     * @param username
     * @return
     */
    @Override
    public List<Map<String, Object>> getMenuList(String username) {
        //下面这个方法需要三表关联查询,好吧,我承认我太菜了,不会一条SQL查完,就分开写了
        /*List<Menu> menuList = userLJMDao.getMenuList(username);*/

        //分多个SQL写,为了方便,所有的SQL都在UserLJMDao.xml里完成
        //获取当前用户id
        Integer userId = userLJMDao.getIdByUserName(username);
        //获取用户对应的角色Id
        Integer roleId = userLJMDao.getRoleIdByUserId(userId);
        //获取角色对应的菜单权限
        List<Menu> menuList = userLJMDao.getMenuList(roleId);
        /*{
                    "path": "2",
                    "title": "会员管理",
                    "icon": "fa-user-md",
                    "children": [
                        {
                            "path": "/2-1",
                            "title": "会员档案",
                            "linkUrl": "member.html",
                            "children": []
                        },
                        {
                            "path": "/2-2",
                            "title": "体检上传",
                            "children": []
                        },
                        {
                            "path": "/2-3",
                            "title": "会员统计",
                            "linkUrl": "all-item-list.html",
                            "children": []
                        },
                    ]
                },*/
        //父菜单
        List<Map<String, Object>> parentMenus = new ArrayList<>();
        //子菜单
        List<Map<String, Object>> sonMenus = new ArrayList<>();
        //获取父菜单
        for (Menu menu : menuList) {
            Map<String, Object> parentMap = new HashMap<>();
            if (menu.getParentMenuId() == null) {
                parentMap.put("path", menu.getPath());
                parentMap.put("title", menu.getName());
                parentMap.put("icon", menu.getIcon());
                //给父元素设置子菜单
                List<Map<String, Object>> sonList = new ArrayList<>();
                for (Menu menu1 : menuList) {
                    Map<String, Object> sonMap = new HashMap<>();
                    if (menu1.getParentMenuId() == menu.getId()) {
                        sonMap.put("path", menu1.getPath());
                        sonMap.put("title", menu1.getName());
                        sonMap.put("linkUrl", menu1.getLinkUrl());
                        sonList.add(sonMap);
                    }
                }
                parentMap.put("children", sonList);
                parentMenus.add(parentMap);
            }
        }
        return parentMenus;
    }
}
